/**
 * 
 */
package fr.fermeManager.application.views.models;

import java.util.GregorianCalendar;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import fr.fermeManager.library.model.beans.Transaction;
import fr.fermeManager.library.model.beans.TypeTransaction;

/**
 * @author Mo
 *
 */
public class ModelGridTransactions extends AbstractTableModel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("rawtypes")
	private final Class[] columnClasses = {Integer.class,Object.class,Float.class,String.class,String.class,TypeTransaction.class,GregorianCalendar.class};
	
	private final String[] entetes = {"Id", "Actif", "Montant", "Libelle", "Description" , "Type" , "Date"};
	
	private List<Transaction> listeTransactions;
	
	
	public ModelGridTransactions(List<Transaction> listeTransactionsParam) {
		listeTransactions = listeTransactionsParam;
	}

	@Override
	public int getRowCount() {
		
		return listeTransactions.size();
	}

	@Override
	public int getColumnCount() {
		return entetes.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return listeTransactions.get(rowIndex).getId();
		case 1:
			return listeTransactions.get(rowIndex).isActif();
		case 2:
			return listeTransactions.get(rowIndex).getMontant();
		case 3:
			return listeTransactions.get(rowIndex).getLibelle();
		case 4:
			return listeTransactions.get(rowIndex).getDescription();
		case 5:
			return listeTransactions.get(rowIndex).getTypeTransaction();
		case 6:
			return listeTransactions.get(rowIndex).getDateCreation();
		default:
			return null;
		}
		
	}
	
	public void ajouterTransaction(Transaction newTransaction){
		this.listeTransactions.add(newTransaction);
		fireTableRowsInserted(listeTransactions.size()-1, listeTransactions.size()-1);
	}
	
	public void supprimerTransaction(int idTransaction) {
		Transaction transacTrouvee = null;
		for (Transaction transacTmp : this.listeTransactions) {
			if(transacTmp.getId() == idTransaction){
				transacTrouvee = transacTmp;
				break;
			}
		}
		int indiceElem = this.listeTransactions.indexOf(transacTrouvee);
		fireTableRowsDeleted(indiceElem, indiceElem);
	}

	@Override
	public String getColumnName(int column) {
		return entetes[column];
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}
	
	public Transaction getElementByIndex(int index){
		return listeTransactions.get(index);
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
	 */
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return columnClasses[columnIndex];
	}

}
